// pages/map/map.js
var QQMapWX = require('../../utils/qqmap-wx-jssdk.js');
var qqmapsdk;
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        scroll: true,
        height: 'auto',
        viewTop: -150,
        startPoint: [0, 0],
        mask: true,
        points: [],
        latitude: 22.81673,
        longitude: 108.3669
    },



    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this
        var name = '电影院'
        wx.getSystemInfo({
            success: function (res) {
                that.setData({
                    height: res.windowHeight
                })
            }

        });
        qqmapsdk = new QQMapWX({
            key: 'RTNBZ-OVFKJ-XNRFE-FRCX3-N2LIV-EQBWJ'
        });

        wx.getLocation({

            type: 'gcj02',
            altitude: true,
            //定位成功，更新定位结果      
            success: function (res) {
                var latitude = res.latitude
                var longitude = res.longitude
                var speed = res.speed
                var accuracy = res.accuracy
                that.setData({ //赋值
                    longitude: longitude,
                    latitude: latitude,
                    Mylongitude: longitude,
                    Mylatitude: latitude,
                    speed: speed,
                    accuracy: accuracy
                })
                //必须setdata结束才能运行搜索函数
                that.nearby_search(name);
                app.globalData.latitude = latitude;
                app.globalData.longitude = longitude;

            }, //定位失败回调      
            fail: function () {
                wx.hideLoading();
                console.log("getLocationFail")
            },
            complete: function () {
                //隐藏定位中信息进度       
                wx.hideLoading()
            }
        })
        console.log(this.data)
        that.getBaiduTaken();


    },
    nearby_search: function (name) {
        var that = this
        console.log(this.data)
        let loc = [this.data.latitude, this.data.longitude]
        // 调用接口
        console.log(loc)
        qqmapsdk.search({
            keyword: name, //搜索关键词
            location: loc.toString(), //设置周边搜索中心点
            success: function (res) { //搜索成功后的回调
                var mks = []
                for (var i = 0; i < res.data.length; i++) {
                    mks.push({ // 获取返回结果，放到mks数组中
                        title: res.data[i].title,
                        id: res.data[i].id,
                        latitude: res.data[i].location.lat,
                        longitude: res.data[i].location.lng,
                        iconPath: "/img/pos.png", //图标路径
                        width: 45,
                        height: 50
                    })
                }
                that.setData({ //设置markers属性，将搜索结果显示在地图中
                    markers: mks
                })
            },
            fail: function (res) {
                console.log(res);
            },
            complete: function (res) {
                console.log(res);
            }
        });
    },
    getBaiduTaken: function () {
        const tokenUrl = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=Ea23fRADac6dMhOxQxbBaphV&client_secret=1Q5vQ2K5qrbL69a4EcPyu8reUSEKXvSp";

        var that = this;
        wx.request({
            url: tokenUrl,
            method: 'POST',
            dataType: "json",
            header: {
                'content-type': 'application/json; charset=UTF-8'
            },
            success: function (res) {
                if (!res.data.access_token) {
                    console.log("【getBaiduTaken提示失败,无效的id&key】");
                } else {
                    console.log("【getBaiduTaken成功】");
                    getApp().globalData.baiduToken = res.data.access_token
                }
            },
            fail: function (res) {
                console.log("【getBaiduTaken提示失败】", res);
            }
        })
    },
    TapClick() {
        var name = '电影院'
        this.nearby_search(name);
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var _this = this;
        var pic = app.globalData.pic;
        //console.log(pic);
        if (pic == null || pic == "undefined") {
            return;
        }

        var mks = []
        var points = [];
        mks.push({ // 获取返回结果，放到mks数组中
            title: pic.name,
            id: pic.id,
            latitude: pic.latitude,
            longitude: pic.longitude,
            iconPath: "/img/pos.png", //图标路径
            width: 45,
            height: 50
        })

        points.push({
            latitude: pic.latitude,
            longitude: pic.longitude
        })

        points.push({
            latitude: _this.data.Mylatitude,
            longitude: _this.data.Mylongitude
        })

        _this.setData({
            markers: mks,
            points: points,
        })

        app.globalData.pic = null;
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})